<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class GameLogDayRepository {
    /**
     * 清空数据表
     */
    public function clearTable() {
        app('model_game_log_day')->truncate();
    }

    /*
    * 一键添加数据
    * @param $param
    * @return bool
    * */
    public function addAll($param) {
        $addAll = app('model_game_log_day')->insert($param);
        if ($addAll) return $addAll;
        throw new BasicException(93085);
    }

    /*
   * 获取日汇总列表
   * @param $where_user
   * @param $where_game
   * @param $perPage
   * @param $page
   * @return bool
   * */
    public function userDailySummaryList($where_game, $where_user, $page = 1, $perPage = 20) {
        $userDailySummaryList = app('model_game_log_day')
            ->where($where_game)
            ->orderBy('summary_date', 'desc')
            ->orderBy('user_id', 'asc')
            ->with('user_data:id,user_id,nickname,user_code,head_picture')
            ->where(function ($query) use ($where_user) {
                $query->whereHas('user_data', function ($query) use ($where_user) {
                    $query->where($where_user);
                });
            })
            ->paginate($perPage, ['*'], '', $page);
        return $userDailySummaryList;
    }


    /*
    * 获取总汇总列表
    * @param $where_user
    * @param $page
    * @param $perPage
    * @return bool
    * @throws BasicException
    * */
    public function totalDailySummaryList($where_user, $page = 1, $perPage = 20) {
        $totalDailySummaryList = app('model_game_log_day')
            ->select('pool_type', 'user_id', 'summary_date', DB::raw('sum(total) as total'), DB::raw('sum(cost) as cost'), DB::raw('sum(number) as number'))
            ->groupBy('user_id')
            ->with('user_data:id,user_id,nickname,user_code,head_picture')
            ->where(function ($query) use ($where_user) {
                $query->whereHas('user_data', function ($query) use ($where_user) {
                    $query->where($where_user);
                });
            })
            ->paginate($perPage, ['*'], '', $page);
        return $totalDailySummaryList;
    }

    /*
    * 获取产出总汇总列表
    * @param $where_game
    * @param $page
    * @param $perPage
    * @return bool
    * @throws BasicException
    * */
    public function  poolTotalSummary($where_game,$page,$perPage){
        $poolTotalSummary = app('model_game_log_day')
            ->select('pool_type', 'user_id', 'summary_date', DB::raw('sum(total) as total'), DB::raw('sum(cost) as cost'), DB::raw('sum(number) as number'))
            ->where($where_game)
            ->groupBy('pool_type')
            ->groupBy('summary_date')
            ->orderBy('summary_date','desc')
            ->paginate($perPage, ['*'], '', $page);
        return $poolTotalSummary;
    }

}

?>